/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Simulation.Messages;

   

    /*
     * Message Layout
     *  0.. 3   byte count
     *  4.. n   DIS data
     */  

public class DisMessage extends Message {
    
    public DisMessage( byte[] data) {                      
        messageID   = Simulation.Messages.Message.MessageIdEnum.DIS_INCOMING_WRAPPED;
        SetDisData(data);
    }  
    
    public DisMessage( Message msg) {                      
        this.messageID = msg.messageID;
        this.messageData = msg.messageData;
    }     
    
    public void SetDisData(byte[] data) {
        int requiredLength;
        int remainder;        
        
        // Work out the required length to hold the DIS data + the wrapper
        requiredLength = data.length;        
        requiredLength += 4;
        remainder = requiredLength % 4;
        if( remainder > 0 )
            requiredLength += (4-remainder);

        // allocate the required room
        messageData = new byte[requiredLength];
        
        // write the DIS data byte count to the first 4 bytes
        byte[] bSize = Simulation.Utils.ByteArrayUtils.ToByte(data.length);
        messageData[0] = bSize[0];
        messageData[1] = bSize[1];
        messageData[2] = bSize[2];
        messageData[3] = bSize[3];        
        
        // copy the dis data
        for( int i=0; i< data.length; i++)
            messageData[4+i] = data[i];
        
        // square off any unused data
        if( remainder > 0 )
            for( int i=0; i< (4-remainder); i++)
                messageData[data.length + 4 + i] = 0;          
    }
    
    public byte[] GetDisData() {
        
        // Get DIS byte count
        byte[] bValue;
        int    disDataByteCount;                 
        bValue = new byte[4];
        bValue[0] = messageData[0];
        bValue[1] = messageData[1];  
        bValue[2] = messageData[2];
        bValue[3] = messageData[3];           
        disDataByteCount = Simulation.Utils.ByteArrayUtils.BytesToInt(bValue);
        
        //Create storage for result;
        byte[] result = new byte[disDataByteCount];
        
        // copy the dis data
        for( int i=0; i< disDataByteCount; i++)
            result[i] = messageData[4+i]; 
        
        return result;
    }    
       
        
}

